package org.example.server.read;

import org.example.models.Server;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class UserReadStrategy extends ReadStrategy {
    @Override
    public void operateData() {
        try {
            //把客户端收到的消息递交给服务器，由服务器转发到各个代理端口内
            Socket socket = new Socket(Server.getMainServerIP(), Server.getMainServerPort());
            String json =
                    "{\n" +
                    "   \"type\": \"server\",\n" +
                    "   \"data\": {\n" +
                    "       \"operation\": \"send\",\n" +
                    "       \"meetingID\": \"" + dataJsonObject.getString("meetingID") + "\",\n" +
                    "       \"message\": \"" + dataJsonObject.getString("message") + "\"\n" +
                    "   }\n" +
                    "}";
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            pw.write(json);
            pw.flush();
            pw.close();
            socket.close();
            this.socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
